package cn.k.service.impl;

import cn.k.domain.MessageBoard;
import cn.k.mapper.MessageBoardMapper;
import cn.k.qo.QueryObject;
import cn.k.service.IMessageBoardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MessageBoardServiceImpl implements IMessageBoardService {
    @Autowired
    private MessageBoardMapper messageBoardMapper;

    @Override
    @Transactional
    public void insert(MessageBoard messageBoard) {
        messageBoard.setCreateTime(new Date());
         messageBoardMapper.insert(messageBoard);
    }

    @Override
    public void delete(Long id) {
        messageBoardMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(MessageBoard messageBoard) {
        messageBoardMapper.updateByPrimaryKey(messageBoard);

    }

    @Override
    public MessageBoard get(Long id) {
        MessageBoard messageBoard = messageBoardMapper.selectByPrimaryKey(id);
        return messageBoard;
    }

    @Override
    public List<MessageBoard> selectAll() {
        List<MessageBoard> messageBoards = messageBoardMapper.selectAll();
        return messageBoards;
    }


    @Override
    public List<MessageBoard> selectForList(QueryObject qo) {
        List<MessageBoard> messageBoards = messageBoardMapper.selectForList(qo);
        return messageBoards;
    }

    @Override
    public PageInfo<MessageBoard> query(QueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询当前页数据
        List<MessageBoard> messageBoards = messageBoardMapper.selectForList(qo);
        return new PageInfo<>(messageBoards);
    }

    @Override
    public MessageBoard selectReplyCount(Long id) {
        return messageBoardMapper.selectReplyCount(id);
    }
}
